package com.adopapa.nezhacloud.ddd.domain;

import java.io.Serializable;

import com.adopapa.nezhacloud.common.Tools;

public class RestResponse implements Serializable {

	private static final long serialVersionUID = 4090874390900329163L;

	public final static int ERR = 600;
	public final static int OK = 601;
	public final static int OTHER = 602;
	public final static int NOAUTH = 604;

	private int code;

	private String message;

	private Object result;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public RestResponse code(int code) {
		this.code = code;
		return this;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public RestResponse message(String message) {
		this.message = message;
		return this;
	}

	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public RestResponse result(Object result) {
		this.result = result;
		return this;
	}

	public static RestResponse ok() {
		return new RestResponse().code(OK).message("请求成功");
	}

	public static RestResponse ok(Object result) {
		return new RestResponse().code(OK).message("请求成功").result(result);
	}

	public static RestResponse ok(String message, Object result) {
		return new RestResponse().code(OK).message(message).result(result);
	}

	public static RestResponse ko(String message) {
		return new RestResponse().code(OK).message(message);
	}

	public static RestResponse other(int code, String message, Object result) {
		return new RestResponse().code(code).message(message).result(result);
	}

	public static RestResponse other(int code, String message) {
		return new RestResponse().code(code).message(message);
	}

	public static RestResponse other(String message) {
		return new RestResponse().code(OTHER).message(message);
	}

	public static RestResponse noauth(String message) {
		return new RestResponse().code(NOAUTH).message(message);
	}

	public static RestResponse error(Throwable e) {
		String message = Tools.empty(e.getMessage()) ? "请求失败,请确认权限！" : e.getMessage();
		return new RestResponse().code(ERR).message(message);
	}

}
